package com.iotechn.unimall.admin.api.tree;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.TreeDO;
import com.iotechn.unimall.data.domain.TreeDetailsDO;
import com.iotechn.unimall.data.domain.TreeSeedDO;
import com.iotechn.unimall.data.mapper.TreeDetailsMapper;
import com.iotechn.unimall.data.mapper.TreeMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


@Service
public class AdminTreeServiceImpl implements AdminTreeService {

    @Autowired
    private TreeMapper treeMapper;
    @Autowired
    private TreeDetailsMapper detailsMapper;

    @Override
    public Page<TreeDO> list(Integer page, Integer limit, String title, String categoryId, Long adminId) throws ServiceException {
        List<TreeDO> TreeDO = treeMapper.selectListByBack(title, categoryId, (page - 1) * limit, limit);
        Long count = treeMapper.countOrder(title, categoryId);
        return new Page<TreeDO>(TreeDO, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TreeDO create(TreeDO treeDO, Long adminId) throws ServiceException {
        if (treeMapper.insert(treeDO) > 0) {
            Long id = treeDO.getId();
            treeDO.setId(id);
            String ids = treeDO.getIds();
            if(ids != null){
                String[] ss = ids.split(",");
                for (String s : ss) {
                    TreeSeedDO treeSeedDO = new TreeSeedDO();
                    treeSeedDO.setTreeId(id);
                    treeSeedDO.setSeedId(Long.valueOf(s));
                   // treeSeedMapper.insert(treeSeedDO);
                }
            }
            return treeDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(TreeDO treeDO, Long adminId) throws ServiceException {
        Long id = treeDO.getId();
        if (id == null) return "fail";
        if (treeMapper.updateById(treeDO) > 0) {

            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        Integer count = detailsMapper.selectCount(new EntityWrapper<TreeDetailsDO>().eq("tree_id", id));
        if(count > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_CANNOT_DELETE);
        if (treeMapper.deleteById(id) > 0) {
           return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public TreeDO detail(Long id, Long adminId) throws ServiceException {
        TreeDO treeDO = treeMapper.selectById(id);
        return treeDO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.TREE_NOT_EXIST);
        if (treeMapper.delete(new EntityWrapper<TreeDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_NOT_EXIST);
        detailsMapper.delete(new EntityWrapper<TreeDetailsDO>().in("tree_id", ids));
        return "ok";
    }

    @Override
    public List<TreeDO> select(Long adminId) {
        Wrapper<TreeDO> wrapper = new EntityWrapper<TreeDO>()
                .setSqlSelect("id", "title").orderBy("id", false);
        return treeMapper.selectList(wrapper);
    }
}
